<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="org.correct.model.PageItem" %>
<%@ page import="org.correct.config.Config" %>
<%@ page import="java.util.Calendar" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="org.correct.correcter.ResultNode;" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<%
	
		String keyword = (String)request.getAttribute("keyword");
		PageItem[] plist = (PageItem[])request.getAttribute("plist");
		String[] kwlist = (String[])request.getAttribute("kwlist");
		int npstart = (Integer)request.getAttribute("pn");
		int nptotal = (Integer)request.getAttribute("nTotal");
		ResultNode correctAndTerms = (ResultNode)request.getAttribute("correctAndTerms");
		boolean errorInKeyword = false;
		StringBuffer buf = new StringBuffer();
		String correctionStr = null;
		
		if(plist == null) plist = new PageItem[0];
		if(kwlist == null) kwlist = new String[0];
		errorInKeyword = (correctAndTerms.haveError != 0);
		if(errorInKeyword) {
			for(String s : correctAndTerms.result)
				buf.append(s);
			correctionStr = buf.toString();
		}
	%>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>Correct!</title>
		<link rel="stylesheet" type="text/css" media="screen" href="css/result.css" />
	</head>
	<body link="#0000cc">
		<table width="100%" height="58" align="center" cellpadding="0" cellspacing="0">
			<tbody>
				<tr valign="middle">
					<td width="100%" valign="top" style="padding:4px 0 0 8px;width:137px;" nowrap>
						<a href="/correct"><img src="image/logo_small.png" border="0" width="137" height="46" alt="回到首页"></a>
					</td>
					<td>&nbsp;&nbsp;&nbsp;</td>
					<td width="100%" valign="top">
						<div class="Tit">&nbsp;&nbsp;&nbsp;</div>
						<table cellspacing="0" cellpadding="0">
							<tbody>
								<tr>
									<td valign="top" nowrap>
										<form action="search" name="f" method="get">
											<input name="wd" id="kw" size="46" class="i" value="<%=(keyword) %>" maxlength="100">
											<input type="submit" name="btnT" id="su" value="Correct!" class="btn">
											&nbsp;&nbsp;&nbsp;
										</form>
									</td>
								</tr>
							</tbody>
						</table>
					</td>
					<td></td>
				</tr>
			</tbody>
		</table>

		<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" class="bi">
			<tr>
				<td nowrap>&nbsp;&nbsp;&nbsp;&nbsp;找到相关结果<%=nptotal %>个</td>
			</tr>
		</table>
		<table width="30%" border="0" cellpadding="0" cellspacing="0" align="right">
			<tr>
			<td align="left" style="padding-right:10px">
			<div style="border-left:1px solid #e1e1e1;padding-left:10px;word-break:break-all;word-wrap:break-word;">
				<div class="r">
					<div style="background:#EFF2FA;overflow:hidden;padding:0 3px;line-height:29px;height:30px;word-break:normal;">
						<a href="http://localhost:8080/correct" target="_blank" style="font-size:16px">
							回到首页
						</a>
					</div>
				</div>
				<br />
			</div>
			<br>
			</td>
			</tr>
		</table>
		<% if(errorInKeyword) { %>
		<p style="margin:0 15px 10px"><strong class=f14>您要找的是不是:<span  class="jc"> 
			<a href="/correct/search?wd=<%=correctionStr %>"><%=correctionStr %></a>&nbsp;
			</span></strong><br></p>
		<br>
		 <% } %> 
			<%
				for(int i = 0; i < plist.length; i++) {
			%>
		<table id="<%=i %>" border="0" cellpadding="0" cellspacing="0">
			<tbody>
				<tr>
					<td	class="f">
						<a href="<%="/correct/search?wd=" + keyword + "&url=" + plist[i].getUrl()%>" target="_blank">
							<font size="3">
							    <%
							     String pTitle = plist[i].getTitle();
							     char[] sTemp = null;
							     String sTempStr = null;
							     for(String s : correctAndTerms.result) {
							    	 pTitle = pTitle.replaceAll(s, "<em>" + s + "</em>");
							    	 sTemp = s.toCharArray();
							    	 sTemp[0] = Character.toUpperCase(sTemp[0]);
							    	 sTempStr = new String(sTemp);
							    	 pTitle = pTitle.replaceAll(sTempStr, "<em>" + sTempStr + "</em>");
							     }
							    %>
								<%=pTitle %>
							</font>
						</a>
						<br>
						<font size="-1">
						    <%
						    String pDesc = plist[i].getDescription();
						    sTemp = null;
						    sTempStr = null;
						    for(String s : correctAndTerms.result) {
						    	
                                pDesc = pDesc.replaceAll(s, "<em>" + s + "</em>");
                                sTemp = s.toCharArray();
                                sTemp[0] = Character.toUpperCase(sTemp[0]);
                                sTempStr = new String(sTemp);
                                pDesc = pDesc.replaceAll(sTempStr, "<em>" + sTempStr + "</em>");
                            }
						    %>
							<%=pDesc %>
							<br>
							<font color="#008000"><%=(plist[i].getUrl().length() > Config.MAX_URL_DISPLAY_LEN ? (plist[i].getUrl().substring(0, Config.MAX_URL_DISPLAY_LEN) + " ...") : plist[i].getUrl()) %></font>&nbsp;
							<font color="#008000">
								<%=plist[i].getFetchTime().get(Calendar.YEAR) %>-<%=plist[i].getFetchTime().get(Calendar.MONTH)+1 %>-<%=plist[i].getFetchTime().get(Calendar.DATE) %>
							</font>
							 - <a href="/correct" target="_blank" class="m">先睹为快</a>
							 &nbsp;&nbsp;
							 <a href="/correct" target="_blank" class="m">类似网页</a>
		 					<br>
		 				</font>
		 			</td>
		 		</tr>
		 	</tbody>
		 </table>
		 <br>
			<%
				}
			%>
		 <br>
		 <div class="p">
		 <%
		 	if(plist.length != 0) {
		 		int current = npstart / Config.ITEM_NUM_PER_PAGE;
		 		int start = (current < 10 ? 0 : current - 10);
		 		int end = start + 9 > (nptotal - 1) / Config.ITEM_NUM_PER_PAGE ?
		 				(nptotal - 1) / Config.ITEM_NUM_PER_PAGE :
		 				start + 9;
		 		for(int i = start; i <= end; i++) {
		 			if(i == current) {
		 %>
		 	<%=(current + 1) %>&nbsp;
		 <%
		 			} else {
		 %>
		 	<a href="/correct/search?wd=<%= keyword %>&pn=<%=i * Config.ITEM_NUM_PER_PAGE %>">[<%=i+1 %>]</a>&nbsp;
		 <%
		 			}
		 		}
		 		if(current < end) {
		 %>
		 	<a href="/correct/search?wd=<%= keyword %>&pn=<%=(current + 1) * Config.ITEM_NUM_PER_PAGE %>"><font size="3">下一页</font></a>&nbsp;
		 <%
		 		}
		 	} else {
		 %>
		 	&nbsp;
		 <%
		 	}
		 %>
		 </div>
		 <%
		 	if(kwlist != null && kwlist.length > 0) {
		 		int index = 0;
		 %>
		 <div style="background-color: rgb(239, 242, 250); height: 60px; width: 100%; clear: both;">
		 	<table width="96%" height="100%" cellspacing="0" cellpadding="0" border="0" align="center">
		 		<tbody>
		 			<tr>
		 				<td style="font-size: 14px; font-weight: bold; height: 35px; width: 70px;">相关搜索</td>
		 				<td valign="middle" rowspan="<%=(kwlist.length > 5 ? 2 : 1) %>">
		 					<table cellspacing="0" cellpadding="0" border="0">
		 						<tbody>
		 							<tr>
		 							<%
		 								while(index < kwlist.length && index < 5) {
		 							%>
		 								<td class="f14" nowrap>
		 									<a href="search?wd=<%=kwlist[index]%>">
		 										<%=kwlist[index] %>
		 									</a>
		 								</td>
		 								<td class="s" nowrap>&nbsp;</td>
		 							<%
		 									index++;
		 								}
		 							%>
		 							</tr>
		 							<%
		 								if(kwlist.length > 5) {
		 							%> 
		 							<tr>
		 							<%
		 									while(index < 10 && index < kwlist.length) {
		 							%>
		 								<td class="f14" nowrap>
		 									<a href="search?wd=<%=kwlist[index]%>">
		 										<%=kwlist[index] %>
		 									</a>
		 								</td>
		 								<td class="s" nowrap>&nbsp;</td>
		 							<%
		 										index++;
		 									}
		 							%>
		 							</tr>
		 							 <%
		 								}
		 							 %>
		 						</tbody>
		 					</table>
		 				</td>
		 			</tr>
		 			<tr><td>&nbsp;&nbsp;</td></tr>
		 		</tbody>
		 	</table>
		 </div>
		 <%
		 	}
		 %>
		 <table style="margin-left: 18px; height: 60px;" cellpadding="0" cellspacing="0">
		 	<tbody>
		 		<tr valign="middle">
		 			<td nowrap="nowrap">
		 				<input name="wd" size="46" class="i" value="<%=(keyword) %>" maxlength="100" id="kw2">
		 				<input name="btnT" value="Correct!" class="btn" type="submit"> &nbsp;&nbsp;&nbsp;
		 			</td>
		 		</tr>
		 	</tbody>
		 </table>
		 <div id="ft">
		 	&#169;2010 Correct!
		 	<span>此内容系Correct!根据您的指令自动搜索的结果，
		 	不代表Correct!赞成被搜索网站的内容或立场
		 	</span>
		 </div>
	</body>
</html>